/*****************************************************************************
 * Copyright (C) Marcelo F. Ochoa. All rights reserved.                      *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 *****************************************************************************/

package com.prism.toolkit;

import oracle.aurora.rdbms.DbmsJava;

/**
 * Empty place holder class to install in a user space and
 * access to his classLoader.
 * Since Oracle 11g classes loaded in SYS schema do not use current connected
 * classloader.
 * So loading this class at the connected user will allow to DBPrism Toolkit
 * a know entry point to get the current class load.
 */
public class Bootstrap {
    public Bootstrap() {
    }

    /**
     * Try to find a Bootstrap class at the current user 
     * (user whos effective is connected to the database 
     *  either using regular or proxy connection)
     * and returns his efective ClassLoader
     * If no Bootstrap class is loaded it will use DBPrism Toolkit ClassLoader:
     *    OracleClassLoader: Resolver((* SYS)(* PUBLIC)),SYS
     * For example loading this class with:
     *    loadjava -u CMS_PUBLIC/CMS_PUBLIC@test -r -v com/prism/toolkit/Bootstrap.java
     * Allows to CMS_PUBLIC, for example, to return at runtime his ClassLoader:
     *    OracleClassLoader: Resolver((* CMS_PUBLIC)(* PUBLIC)),CMS_PUBLIC
     * @return ClassLoader
     */
    public static ClassLoader getClassLoader() {
        String user = Jowa.GETCGIVAR("REMOTE_USER").toUpperCase();
        Class clazz = Bootstrap.class;
        try {
            //System.out.println(".Bootstrap - looking "+Bootstrap.class.getName()+" in schema: "+user);
            clazz = DbmsJava.classForNameAndSchema (Bootstrap.class.getName(), user);
        } catch (ClassNotFoundException e) {
            System.err.println(".Bootstrap - Unable to find Bootstrap class in schema: "+user);
            e.printStackTrace();
            System.err.println("   Try to load it using: ");
            System.err.println("   loadjava -u "+user+"/***@db -r -v com/prism/toolkit/Bootstrap.java");
            System.err.println(" returning system Bootstrap: "+Bootstrap.class.getClassLoader().toString());
        }
        //System.out.println(".Bootstrap - returning classloader: "+clazz.getClassLoader().toString());
        return clazz.getClassLoader();
    }
}
